package com.aruba.room;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import java.util.List;

/**
 * Created by aruba on 2021/9/12.
 */
public class UserViewModel extends AndroidViewModel {
    private UserRepository userRepository;

    public UserViewModel(@NonNull Application application) {
        super(application);
        userRepository = new UserRepository(application);
    }

    public void insert(View v) {
        userRepository.insert(new User("张三", 12));
    }

    public void update(View v) {
        User user = new User("赵四", 18);
        user.id = 1;
        userRepository.update(user);
    }

    public void delete(View v) {
        User user = new User();
        user.id = 2;
        userRepository.delete(user);
    }

    public void deleteAll(View v) {
        userRepository.deleteAllUser();
    }
    
    public LiveData<List<User>> getUsers() {
        return userRepository.query();
    }
}
